using System.Linq.Expressions;
using OneCaps.Core.Entity;

namespace OneCaps.Core.Interfaces
{
    /// <summary>
    /// 通用仓库接口
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    /// <typeparam name="TKey">主键类型</typeparam>
    public interface IRepository<T, TKey> where T : BaseEntity<TKey>
    {
        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        Task<T?> GetByIdAsync(TKey id);

        /// <summary>
        /// 根据条件获取实体
        /// </summary>
        Task<T?> GetAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 获取所有实体
        /// </summary>
        Task<IEnumerable<T>> GetAllAsync();

        /// <summary>
        /// 根据条件查询实体
        /// </summary>
        Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 分页查询
        /// </summary>
        Task<(IEnumerable<T> Items, int TotalCount)> GetPagedAsync(
            Expression<Func<T, bool>>? predicate = null,
            Expression<Func<T, object>>? orderBy = null,
            bool ascending = true,
            int page = 1,
            int pageSize = 20);

        /// <summary>
        /// 添加实体
        /// </summary>
        Task AddAsync(T entity);

        /// <summary>
        /// 批量添加实体
        /// </summary>
        Task AddRangeAsync(IEnumerable<T> entities);

        /// <summary>
        /// 更新实体
        /// </summary>
        Task UpdateAsync(T entity);

        /// <summary>
        /// 删除实体
        /// </summary>
        Task DeleteAsync(TKey id);

        /// <summary>
        /// 删除实体
        /// </summary>
        Task DeleteAsync(T entity);

        /// <summary>
        /// 批量删除实体
        /// </summary>
        Task DeleteRangeAsync(IEnumerable<T> entities);

        /// <summary>
        /// 检查实体是否存在
        /// </summary>
        Task<bool> ExistsAsync(TKey id);

        /// <summary>
        /// 检查实体是否存在
        /// </summary>
        Task<bool> ExistsAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 获取实体数量
        /// </summary>
        Task<int> CountAsync();

        /// <summary>
        /// 获取实体数量
        /// </summary>
        Task<int> CountAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 保存更改
        /// </summary>
        Task<int> SaveChangesAsync();

        /// <summary>
        /// 批量更新实体
        /// </summary>
        Task UpdateRangeAsync(IEnumerable<T> entities);
    }
} 